*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Repeated Simulations 											~~~~
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Install required packages
	* ssc install xtdpdml
	* ssc install xtabond2
	* findit frmttable

* Defining paths
global dofiles = "C:\Simulations\Code"
global results = "C:\Simulations\Code\Results"

clear all	
set more off
cd "$dofiles"
	
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Setting number of observations (n), waves (t), runs (iterations)
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global obs		= 500		// number of observations
global waves 	= 5			// number of waves
global reps		= 500		// number of runs
		
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Specifying parameters for data-generating process
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global corrXY	= .5	// initial correlation of X and Y
global corrXZ 	= 0		// initial correlation of X and Z
global corrYZ 	= 0 	// initial correlation of Y and Z

* Loop over different scenarios (change to replicate robustness checks)
foreach lambda of numlist 0 0.5 1 {	// contemp. and/or lagged effect 						
foreach endo of numlist 0 0.5 {		// reverse causality 				
foreach autox of numlist 0 {		// autocorrelation of x						
foreach autoy of numlist 0.5 {		// autocorrelation of y									
foreach unob of numlist 0 0.5  {	// time-invariant unob. heterogeneity	

	global autox	= `autox'	// autocorrelation of x
	global endo		= `endo'	// reverse causality (effect of y on x)			
	global autoy	= `autoy'	// autocorrelation of y
	global betax	= 1			// contemporaneous effect of x on y
	global betaxlag	= 1			// lagged effect of x on y	
	global lambdax	= `lambda'	// ratio between cont./lagged effect	
	global unob 	= `unob'	// time-invariant unob. heterogeneity
							
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Run program that creates data and specifies the model estimation
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
run "1_simprogram.do"

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Monte Carlo Simulations
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Saving estimates, standard errors, and confidence intervals
global estimates ""
foreach model in 										///
	POLScon POLSlag POLSboth_con POLSboth_lag 			///
	REcon RElag REboth_con REboth_lag 					///
	FEcon FElag FEboth_con FEboth_lag 					///
	FDcon FDlag FDboth_con FDboth_lag 					///
	ABcon ABlag ABboth_con ABboth_lag 					///									
	MLSEMcon MLSEMlag MLSEMboth_con MLSEMboth_lag  {
	global estimates "$estimates `model'=r(`model') `model'se=r(`model'se) `model'lb95=r(`model'lb95) `model'ub95=r(`model'ub95)"
}

* Running the simulation
simulate "$estimates" , reps($reps) seed(123456) : sim, obs($obs) waves($waves)

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Tabulating results
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	run "2_tables_POLS-RE-FE.do"		// POLS, RE, FE models
	run "3_tables_FD-AB-MLSEM.do"		// FD, AB, ML-SEM models
		
}
}
}
}
}		
	
	
	
	